package com.pengtu.gsj.service.impl.check.lwfw;

import java.util.ArrayList;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONObject;
import com.pengtu.gsj.dao.lwfw.EmergDrillDao;
import com.pengtu.gsj.entity.app.Attachment;
import com.pengtu.gsj.entity.check.CheckTable;
import com.pengtu.gsj.entity.lwfw.EmergDrill;
import com.pengtu.gsj.service.check.CheckTableService;
import com.pengtu.gsj.service.lwfw.EmergPatCheckService;
import com.pengtu.gsj.service.system.SystemManageService;
import com.pengtu.gsj.utils.DateUtils;
import com.pengtu.gsj.utils.MathsUtils;

@Service
@Transactional
public class EmergPatCheckServiceImpl implements EmergPatCheckService{

	@Autowired
	public SystemManageService systemManageService;
	@Autowired
	public CheckTableService checkTableService;
	@Autowired
	public EmergDrillDao emergDrillDao;
	
	
	@Override
	public Map<String, Object> getEmergPatChecks(String startTime, String endTime, String countryOrProvince, CheckTable checkTable, String tag) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		List<String> tempList = null;
		List<EmergDrill> emergPatCheckLists = null;
		List<Attachment> list = new ArrayList<Attachment>();
		List<String> checkMarks = Arrays.asList("0", "0", "0", "0");
		List<String> checkYears = Arrays.asList("0", "0", "0", "0", "0");
		List<List<String>> tempLists = new ArrayList<List<String>>();
		List<List<String>>  resultLists = new ArrayList<List<String>>();
		
		JSONObject jsonObject = JSONObject.parseObject(checkTable.getRetrieval());
		double yearFull = Double.parseDouble((String)jsonObject.get("yearFull"));
		double yearPart = Double.parseDouble((String)jsonObject.get("yearPart"));
		double poitMark = Double.parseDouble((String)jsonObject.get("poitMark"));
		double percentage = 0; //完善率
		double scoreOne = 0; //第一项评分
		double scoreTwo = 0; //第二项评分
		double scoreThree = 0; //第三项评分
		double total = 0; //计算总分
		double tableFull = MathsUtils.formatDouble(yearFull + yearPart + poitMark, 1);
		
		//根据时间、标签查询是否有附件
		list = systemManageService.getAllCheckAttachments(startTime, endTime, tag);
		//1、根据应急预案和现场预案的文件算分，两者都有满分，一个没有扣1/3，都没有不得分
		double tempOne = systemManageService.getAllCheckAttachments("txyjbt_yjya").size();
		double tempTwo = systemManageService.getAllCheckAttachments("txyjbt__xcya").size();
		if (tempOne > 0 && tempTwo > 0) {
			scoreOne = yearFull;
			map.put("opearFlag", "strong");
		} else if (tempOne > 0 || tempTwo > 0) {
			scoreOne = 2*yearFull/3;
			map.put("opearFlag", "middle");
		}
		
		//2、根据应急运行机制的附件中名字是否包含对应的关键词，判断是否得分
		List<Attachment> attLists = systemManageService.getAllCheckAttachments("txyjbt_yjyxjz");
		double eacheScore = MathsUtils.divDouble(yearPart, 4, 1);
		//遍历相关机制附件，如果有就得分,indexOf包含方法没有就返回-1，有就返回整数，使用checkMark标记，由附件得分后不是重复记分
		for (Attachment att : attLists) {
			if ("0" == checkMarks.get(0)){
				if (ifIndexOf(att, "预测", "预警")) {
					checkMarks.set(0, "1");
				}
			}
			if ("0" == checkMarks.get(1)){
				if (ifIndexOf(att, "应急", "处置")) {
					checkMarks.set(1, "1");
				}
			}
			if ("0" == checkMarks.get(2)){
				if (ifIndexOf(att, "信息", "发布")) {
					checkMarks.set(2, "1");
				}
			}
			if ("0" == checkMarks.get(3)){
				if (ifIndexOf(att, "跨部门", "联动")) {
					checkMarks.set(3, "1");
				}
			}
		}
		//在每次的判断之后都进行一次数组的初始化操作
		String str = "";
		for (int i = 0; i < 4; i++) {
			if(i == 0) {
				str = "预测预警机制";
			}
			if (i == 1) {
				str = "应急处置机制";
			}
			if (i == 2) {
				str = "信息发布机制";
			}
			if (i == 3) {
				str = "跨部门联动机制";
			}
			if ("1" == checkMarks.get(i)) {
				tempList = setTempLists(str, "完善", String.valueOf(eacheScore));
				tempLists.add(tempList);
			} else {
				tempList = setTempLists(str, "不完善", "0");
				tempLists.add(tempList);
			}
		}
		for (String temp : checkMarks) {
			scoreTwo += (temp == "1" ? eacheScore : 0);
		}
		
		//3、根据每年是否有对应的文件，判断是否得分
		//根据国省检标记判断是国检还是省检
		int temp = "0".equals(countryOrProvince) ? 5 : 2;
		for (int i = 0; i < temp; i++){
			List<String> checkResult = Arrays.asList(String.valueOf(Integer.parseInt(startTime)+i), "", "", "", "0");
			emergPatCheckLists = emergDrillDao.getAll("drillTime", true);
			for (EmergDrill emergDrill : emergPatCheckLists) {
				if (emergDrill.getYear().equals(String.valueOf(Integer.parseInt(startTime)+i))) {
					checkResult.set(0, emergDrill.getYear());
					checkResult.set(1, emergDrill.getDrillName()+" - "+emergDrill.getDrillPlace());
					checkResult.set(2, DateUtils.convertDateToString(emergDrill.getDrillTime()));
					checkResult.set(3, "完整");
					checkResult.set(4, String.valueOf(MathsUtils.divDouble(poitMark, temp, 1)));
					checkYears.set(i, String.valueOf(Integer.parseInt(startTime)+i));
				}
			}
			resultLists.add(checkResult);
		}
		
		//遍历记录年份的数组，不为“0”就得分
		if ("0".equals(countryOrProvince)) {
			for (String checkScore : checkYears) {
				scoreThree += checkScore == "0" ? 0 : poitMark / 5;
			}
		} else {
			for (String checkScore : checkYears) {
				scoreThree += checkScore == "0" ? 0 : poitMark / 2;
			}
		}
		total = MathsUtils.formatDouble(scoreOne + scoreTwo + scoreThree, 1);
		percentage = MathsUtils.divDouble(total*100, tableFull, 2);
		
		//只有当系统完善度和检查得分不相等的情况下才更新数据  避免每次搜索都更新数据
		checkTableService.updateCheckTable(checkTable, percentage, total, tableFull, true);
		map.put("att", list);
		map.put("checkLists", tempLists);
		map.put("resultLists", resultLists);
		map.put("checkType", countryOrProvince);
		map.put("startTime", Integer.parseInt(startTime));
		map.put("emergPatCheckLists", emergPatCheckLists);
		map.put("scoreOne", MathsUtils.formatDouble(scoreOne, 1));
		map.put("percent", percentage);
		map.put("score", checkTable.getTestFull());
		map.put("checkTable", checkTable);
		return map;
	}
	
	
	/**
	 * checkIndexOf:
	 * 适用:
	 * @param att 单个附件
	 * @param tipOne 关键词1
	 * @param tipTwo 关键词2
	 * @return 附件名称中是否包含关键词1或关键词2
	 * @exception 
	 * @since  1.0.0
	*/
	private static Boolean ifIndexOf(Attachment att, String tipOne, String tipTwo){
		boolean ifIndexOf  = att.getFileName().indexOf(tipOne) + att.getFileName().indexOf(tipTwo) > -1;
		return ifIndexOf;
	}
	
	/**
	 * tempLists:
	 * 适用:
	 * @param mechanism 机制名称
	 * @param ifPerfect 是否完善
	 * @param score 得分
	 * @return 
	 * @exception 
	 * @since  1.0.0
	*/
	private static List<String> setTempLists(String mechanism, String ifPerfect, String score){
		List<String> tempList = Arrays.asList("0", "0", "0");
		tempList.set(0, mechanism);
		tempList.set(1, ifPerfect);
		tempList.set(2, score);
		return tempList;
	}
}
